package borge.web.teamspirit.client.page.team.panels;

import java.util.Date;

import borge.web.teamspirit.client.SessionData;
import borge.web.teamspirit.client.data.Person;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.PersistentData.DataKey;
import borge.web.teamspirit.client.page.shared.widgets.LoggedInWidget;
import borge.web.teamspirit.client.page.shared.widgets.LoginWidget;
import borge.web.teamspirit.client.servercomm.DBQueryResults;
import borge.web.teamspirit.client.servercomm.ServerComm;
import borge.web.teamspirit.client.servercomm.DBQueryResults.Result;

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;

public class TopPanel extends HorizontalPanel implements WidgetEventHandler, ServerCallbackHandler {
	
	
	private LoginWidget iLoginWidget;
	private LoggedInWidget iLoggedInWidget;
	private Image image;
	
	public TopPanel(){
		
		String sessId = Cookies.getCookie("sid");
		
		if(sessId != null)
			ServerComm.isSessionValid(sessId, this);
		else
			switchToLoginWidget();
			
		image = new Image();
		image.setUrl("top2.jpg");
		add(image);
		
	}
	
	public void switchToLoginWidget(){

		if(iLoginWidget == null){
			iLoginWidget = new LoginWidget(this, "150px");
			iLoginWidget.addStyleName("TopPanel");
		}
		else
			iLoginWidget.reset();
		if(getWidgetCount() > 1)
			remove(0);
		
		insert(iLoginWidget, 0);

	}
	
	public void switchToLoggedInWidget(String username){
		
		if(iLoggedInWidget == null)
			iLoggedInWidget = new LoggedInWidget(this, username);
		else
			iLoggedInWidget.setUsername(username);
		
		if(getWidgetCount() > 1)
			remove(0);
		
		insert(iLoggedInWidget, 0);
	}
	
	public void handleWidgetEvent(WidgetEventType aEvent){
		
		switch(aEvent){
		
		case LOGGED_IN: {
			
			// TODO: input should be verified somewhere first!
			ServerComm.loginUser(iLoginWidget.getUsername(), iLoginWidget.getPassword(), this);
			/*
			remove(iLoginWidget);
			if(iLoggedInWidget == null)
				iLoggedInWidget = new LoggedInWidget(this, iLoginWidget.getUsername());
			else
				iLoggedInWidget.setUsername(iLoginWidget.getUsername());
			add(iLoggedInWidget);
			*/
			break;
		}
		
		case LOGGED_OUT: {
			
			ServerComm.destroyUserSession(Cookies.getCookie("sid"), this);
			//switchToLoginWidget();
			/*
			remove(iLoggedInWidget);
			iLoginWidget.reset();
			add(iLoginWidget);
			*/
			break;
			
		}
		
		}
		
	}

	@Override
	public  void handleServerCallback(
			DBQueryResults aResult, CallbackType aType) {
		// TODO Auto-generated method stub
		
		switch(aType){
		
		case SESSION_VALIDITY: {
			
			if(!aResult.querySucceeded())
				switchToLoginWidget();
			else{
				Result result = aResult.getResult(DataKey.PERSON);
				SessionData.sLoggedInUser = (Person)result.getResultList().get(0);
				switchToLoggedInWidget(SessionData.sLoggedInUser.getUserName());
				
			}
			
			break;
			
		}
		
		case USER_LOGGED_IN: {
			
			// success
			if(aResult.getNumResults() > 0){
				Result result = aResult.getResult(DataKey.PERSON);
				Person person = (Person)result.getResultList().get(0);
				SessionData.sLoggedInUser = person;
				
				final long duration = 1000 * 60 * 60 * 24;
				Date expiration = new Date(System.currentTimeMillis() + duration);
				Cookies.setCookie("sid", person.getSessionId(), expiration, null, "/", false);
				
				switchToLoggedInWidget(iLoginWidget.getUsername());
			
			}
			// failed
			else{
				Window.alert("Login failed.");
			}
			
			break;
		}
		
		case USER_LOGGED_OUT: {
			
			Cookies.removeCookie("sid");
        	SessionData.sLoggedInUser = null;
			switchToLoginWidget();
			
			break;
		}
		
		
		}
		
	}

	@Override
	public void handleServerError(Throwable t) {
		// TODO Auto-generated method stub
		
	}
	

}

